|
1
|
|
|
import { localizedField, localizedFieldNonNull } from "../models/app"; |
|
2
|
|
|
|
|
3
|
|
|
export type Locales = "en" | "fr"; |
|
4
|
|
|
type TranslatableKeysNonNull<T> = { |
|
5
|
|
|
[K in keyof T]: T[K] extends localizedFieldNonNull ? K : never; |
|
6
|
|
|
}[keyof T]; |
|
7
|
|
|
type TranslatableKeys<T> = { |
|
8
|
|
|
[K in keyof T]: T[K] extends localizedField ? K : never; |
|
9
|
|
|
}[keyof T]; |
|
10
|
|
|
|
|
11
|
|
|
export function localizeField<T>( |
|
12
|
|
|
locale: Locales, |
|
13
|
|
|
model: T, |
|
14
|
|
|
field: TranslatableKeys<T>, |
|
15
|
|
|
): string | null { |
|
16
|
|
|
if (model[field] !== null) { |
|
17
|
|
|
return model[field][locale]; |
|
18
|
|
|
} |
|
19
|
|
|
return null; |
|
20
|
|
|
} |
|
21
|
|
|
export function localizeFieldNonNull<T>( |
|
22
|
|
|
locale: Locales, |
|
23
|
|
|
model: T, |
|
24
|
|
|
field: TranslatableKeysNonNull<T>, |
|
25
|
|
|
): string { |
|
26
|
|
|
// Even though we assume field is non-null... check anyway to avoid crashes. |
|
27
|
|
|
const value = model[field] ? model[field][locale] : null; |
|
28
|
|
|
if (value) { |
|
29
|
|
|
return value; |
|
30
|
|
|
} |
|
31
|
|
|
return locale === "en" ? "TRANSLATION MISSING" : "TRADUCTION MANQUANTE"; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
export function getLocale(locale: string): Locales { |
|
35
|
|
|
if (locale === "en" || locale === "fr") { |
|
36
|
|
|
return locale; |
|
37
|
|
|
} |
|
38
|
|
|
console.log("Warning: unknown locale. Defaulting to en."); |
|
39
|
|
|
return "en"; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
export function matchValueToModel<T>( |
|
43
|
|
|
locale: Locales, |
|
44
|
|
|
field: TranslatableKeys<T>, |
|
45
|
|
|
value: string, |
|
46
|
|
|
possibilities: T[], |
|
47
|
|
|
): T | null { |
|
48
|
|
|
const matching = possibilities.filter( |
|
49
|
|
|
(model) => localizeField(locale, model, field) === value, |
|
50
|
|
|
); |
|
51
|
|
|
return matching.length > 0 ? matching[0] : null; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
export function matchStringsCaseDiacriticInsensitive( |
|
55
|
|
|
needle: string, |
|
56
|
|
|
haystack: string[], |
|
57
|
|
|
): string[] { |
|
58
|
|
|
return haystack.filter((name) => { |
|
59
|
|
|
return ( |
|
60
|
|
|
name |
|
61
|
|
|
.normalize("NFD") // Normalizing to NFD Unicode normal form decomposes combined graphemes into the combination of simple ones. |
|
62
|
|
|
.replace(/[\u0300-\u036f]/g, "") // Using a regex character class to match the U+0300 → U+036F range, it is now trivial to globally get rid of the diacritics, which the Unicode standard conveniently groups as the Combining Diacritical Marks Unicode block. |
|
63
|
|
|
.search(new RegExp(needle, "i")) !== -1 || |
|
64
|
|
|
name.search(new RegExp(needle, "i")) !== -1 |
|
65
|
|
|
); |
|
66
|
|
|
}); |
|
67
|
|
|
} |
|
68
|
|
|
|